package ij.hkust.gui.laserCtr.DAOSTORM;

import ij.hkust.UserSettings;

import java.awt.Color;
import java.awt.Point;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.plaf.basic.BasicInternalFrameUI;

public class DSTEditor extends JInternalFrame {

	private static final long serialVersionUID = 1L;

	public static DSTEditor instance = new DSTEditor();
	private JPanel contentPane;
	private Point laserLocation = new Point(86, 70);

	//the lasers we need
	private ArrayList<DSTLaser> lasers = new ArrayList<DSTLaser>();
	/**
	 * Create the frame.
	 */
	private DSTEditor() {
		setToolTipText("This is the daostorm setting");
		setBorder(new EmptyBorder(0, 0, 0, 0));

		this.setResizable(false);
		this.setBounds(1, 1, 778, 475);
		((BasicInternalFrameUI)getUI()).setNorthPane(null);
//		this.setBorder(new LineBorder(new Color(128, 128, 128), 2));
		contentPane = new JPanel();
//		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);

		
		JButton btnNewButton = new JButton("AddLaser");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addLaser("","0");//empty name and intensity
			}
		});
		btnNewButton.setBounds(10, 36, 100, 22);
		contentPane.add(btnNewButton);
		
		JButton btnFinish = new JButton("Save");
		btnFinish.setBackground(SystemColor.activeCaption);
		btnFinish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//here we add the settings into the UserSettings
				saveSettings();
			}
		});
		btnFinish.setBounds(10, 10, 0, 0);
		contentPane.add(btnFinish);
		
		
		readFromSettings();
		this.setTitle("Check daostorm's settings");
	}
	
	private void addLaser(String laser,String intensity) {
		
		if(!isCanChange(lasers.size(),laser)){
			return;
		}
		DSTLaser newLaser = new DSTLaser(laser,intensity);
		newLaser.setIndex(lasers.size());
		newLaser.setBounds(20, laserLocation.y, 740, 35);
		contentPane.add(newLaser);
		changeSelect(lasers.size());
		lasers.add(newLaser);
		
		laserLocation.y = laserLocation.y + 37;

		// reset the window's size
//		this.setSize(this.size().width, this.size().height + 50);
		refresh();
	}
	
	/**
	 * save settings to UserSetting
	 */
	public void saveSettings(){
		//clear all the old DAOSTORM laser settings
		UserSettings.dstLaserInt.clear();
		UserSettings.dstLasers.clear();
		//add the new settings to the settings
		int count = 0;
		while(count < lasers.size()){
			DSTLaser tmp = lasers.get(count);
			UserSettings.addDstCtr(tmp.getLaser(), tmp.getIntensity());
			count ++;
		}
		UserSettings.saveSettings();
		//clear the current ui
		while(lasers.size()>0){
			contentPane.remove(lasers.get(0));
			lasers.remove(0);
		}
		laserLocation = new Point(86, 70);
		readFromSettings();
	}
	
	/**
	 * read the usersettings and show them in the window
	 */
	public void readFromSettings(){
		while(lasers.size() > 0){
			delLaser(0);
		}
		for(int i=0;i<UserSettings.dstLasers.size();i++){
			addLaser(UserSettings.dstLasers.get(i),UserSettings.dstLaserInt.get(i));
		}
	}

	/**
	 * change the selected laser
	 * @param index
	 */
	public void changeSelect(int index){
		//here if we do not check, there will be a endless-loop
		if(index>=0 && index < lasers.size()){
			for(int i=0;i<lasers.size();i++){
				if(i != index){
					lasers.get(i).setSelected(false);
				}
			}
			if(!lasers.get(index).isSelected()){
				lasers.get(index).setSelected(true);
			}
		}else{
			//TODO do something
		}
	}
	
	/**
	 * delete the selected laser
	 * @param la
	 */
	public void delLaser(int index){
		if(index >=0 && index < lasers.size()){
			this.contentPane.remove(lasers.get(index));
			lasers.remove(index);
			laserLocation.y=70;
			for(int i=0;i<lasers.size();i++){
				lasers.get(i).setBounds(20, laserLocation.y, 740, 35);
				lasers.get(i).setIndex(i);
				laserLocation.y += 37;
			}
			refresh();
		}else{
			//TODO do something
		}
		
	}
	
	/**
	 * before we change one laser's ID, we should check if there already have the same laser
	 * @param index
	 * @param laser
	 * @return
	 */
	public boolean isCanChange(int index,String laser){
		for(int i=0;i<lasers.size();i++){
			if(i!=index && lasers.get(i).getLaser().equals(laser) && !lasers.get(i).getLaser().equals("")){
				return false;
			}
		}
		return true;
	}
	
	public void refresh(){
		this.hide();
		this.show();
	}
}